package com.xnew.role.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.constant.Constant;
import com.xnew.role.bean.AuRole;
import com.xnew.bean.common.AuUserRole;
import com.xnew.role.mapper.AuRoleMapper;
import com.xnew.role.mapper.AuUserRoleMapper;
import com.xnew.utils.DataInput;
import com.xnew.utils.IdUtils;
import com.xnew.utils.StringUtils;
import com.xnew.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Component
@Transactional(rollbackFor = Exception.class)
public class AuRoleService {

	@Autowired
	private AuRoleMapper auRoleMapper;

	@Resource
	private AuUserRoleMapper auUserRoleMapper;

	/**
	 * 查询角色列表
	 * @param input
	 * @param request
	 * @return
	 */
	public List<AuRole> querySysRoleList(DataInput input, HttpServletRequest request) {
		if (input.getPageNum() != null && input.getPageSize() != null){
			PageHelper.startPage(input.getPageNum(), input.getPageSize());
		}
		return auRoleMapper.querySysRoleList(input.getParam());
	}

	/**
	 * 根据id查询角色信息
	 * @param arId
	 * @return
	 * @throws Exception
	 */
	public AuRole selectByPrimaryKey(String arId) {
		AuRole auRole = auRoleMapper.selectByPrimaryKey(arId);
		List<AuUserRole> auUserRoles = auUserRoleMapper.queryUserRoleListByArId(arId);
		String auIds = "";
		if (auUserRoles.size()>0) {
			for (AuUserRole auUserRole : auUserRoles) {
				auIds = auIds + auUserRole.getAuId() + ",";
			}
			if (auIds.length()>0){
				auIds = auIds.substring(0,auIds.length()-1);
			}
			auRole.setAuIds(auIds);
		}
		return auRole;
	}

	/**
	 * 根据id删除角色信息
	 * @param arId
	 */
	public void deleteSysRoleByid(String arId) {
		auRoleMapper.deleteSysRoleByid(arId);
	}

	/**
	 * 新增或者修改
	 * @param auRole
	 * @throws Exception
	 */
	public void addOrUpdateRole(AuRole auRole, HttpServletRequest request) throws Exception {
		SysUserInfo userInfo = UserUtil.getUserInfo(request);
		if (StringUtils.isBlank(auRole.getArId())) {
			addRole(auRole, userInfo);
		} else {
			updateRole(auRole, userInfo);
		}
	}

	public void addRole(AuRole auRole, SysUserInfo userInfo) throws Exception {
		auRole.setArId(IdUtils.getIncreaseIdByNanoTime());//id
		auRole.setArCreateTime(DateUtil.now());//创建时间
		auRole.setArCreateUser(userInfo.getLoginName());//创建人
		auRoleMapper.insert(auRole);
	}

	public void updateRole(AuRole auRole, SysUserInfo userInfo) throws Exception {
		auRoleMapper.updateByPrimaryKey(auRole);
	}

	/**
	 * 新增个人用户角色权限
	 * @param auRole
	 * @param request
	 */
	public void saveIndvRoleUser(AuRole auRole, HttpServletRequest request) throws Exception {
		SysUserInfo userInfo = UserUtil.getUserInfo(request);
		//auUserRoleMapper.deleteByArId(auRole.getArId());
		String[] auIds = auRole.getAuIds().split(",");
		if (auIds.length>0){
			for (String auId : auIds) {
				AuUserRole aurIsUser = auUserRoleMapper.selectIsUser(auId,auRole.getArId());
				if(aurIsUser== null){
					AuUserRole auUserRole = new AuUserRole();
					auUserRole.setAuId(IdUtils.getIncreaseIdByNanoTime());//id
					auUserRole.setArId(auRole.getArId());//角色id
					auUserRole.setAuId(auId);//用户id
					auUserRole.setAurType(Constant.USER_TYPE_CODE_01);//个人用户类型
					auUserRole.setAurCreateUser(userInfo.getUserInfoNo());//创建人
					auUserRole.setAurCreateTime(DateUtil.now());//创建时间
					auUserRoleMapper.insert(auUserRole);
				}
			}
		}
	}

	/**
	 * 新增机构用户角色权限
	 * @param auRole
	 * @param request
	 */
	public void saveOrgRoleUser(AuRole auRole, HttpServletRequest request) throws Exception {
		SysUserInfo userInfo = UserUtil.getUserInfo(request);
		//auUserRoleMapper.deleteByArId(auRole.getArId());
		String[] auIds = auRole.getAuIds().split(",");
		if (auIds.length>0){
			for (String auId : auIds) {
				AuUserRole aurIsUser = auUserRoleMapper.selectIsUser(auId,auRole.getArId());
				if(aurIsUser== null){
					AuUserRole auUserRole = new AuUserRole();
					auUserRole.setAuId(IdUtils.getIncreaseIdByNanoTime());//id
					auUserRole.setArId(auRole.getArId());//角色id
					auUserRole.setAuId(auId);//用户id
					auUserRole.setAurType(Constant.USER_TYPE_CODE_02);//机构用户类型
					auUserRole.setAurCreateUser(userInfo.getUserInfoNo());//创建人
					auUserRole.setAurCreateTime(DateUtil.now());//创建时间
					auUserRoleMapper.insert(auUserRole);
				}
			}
		}
	}

	/**
	 *根据角色 用户信息删除
	 * @param arId //角色Id
	 * @param auIds //用户Id
	 * @throws Exception
	 */
	public void deleteRoleUser(String arId,String auIds) throws Exception {
		if(auIds.length()>0){
			String[] auidtos = auIds.split(",");
			List<String> strList = new ArrayList<String>();
			for (String auidto : auidtos) {
				strList.add(auidto);
			}
			if(strList.size()>0){
				auUserRoleMapper.deleteRoleUser(arId,strList);
			}
		}
	}
}
